<template>
  <view>
    <!-- 搜索自定义组件 -->
    <view class="search-box">
      <!-- 使用自定义的搜索组件 -->
      <my-search borderRadius="10" bgc="pink" @click="goToSearch"></my-search>
    </view>
    
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <!-- 循环渲染轮播图的item -->
      <swiper-item v-for="(item,index) in swiperList" :key="item.goods_id">
        <!-- 添加navigator跳转详情页 -->
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
          <!-- 动态绑定image的src属性 -->
          <image :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item,i) in navList" :key='i' @click="navClickHandler(item)">
        <image :src="item.image_src" mode=""></image>
      </view>
    </view>

    <!-- 楼层数据 -->
    <view class="floor-list">
      <!-- 每一个楼层 -->
      <view v-for="(item,index) in floorList" :key="index" class="floor-item">
        <!-- 楼层的标题 -->
        <image :src="item.floor_title.image_src" mode="" class="floor-title"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <navigator class="left-img-box" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}"
              mode="widthFix"></image>
          </navigator>
          <view class="right-img-box">
            <navigator v-for="(item2,index2) in item.product_list" :key="index2" v-if="index2 !=0" :url="item2.url">
              <image :src="item2.image_src" :style="{width:item2.image_width+'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 轮播图的列表数据
        swiperList: [],
        // 分类的数据
        navList: [],
        // 楼层数据
        floorList: []
      };
    },
    onLoad() {
      // 一启动就调用轮播数据
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      // 获取轮播图的数据
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata');
        // console.log(res)
        // 请求失败
        if (res.meta.status != 200) {
          /* return uni.showToast({
            title: "数据请求失败",
            duration: 2000,
            icon: none
          }) */
          return uni.$showMsg();
        }
        // 请求成功：将轮播数据挂载在swiperlist上
        this.swiperList = res.message
        return uni.$showMsg("数据请求成功")
      },
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems');
        // console.log(res)
        if (res.meta.status != 200) {
          return uni.$showMsg()
        }
        this.navList = res.message
        return uni.$showMsg("数据请求成功")
      },
      // 点击分类事件
      navClickHandler(item) {
        // console.log(item)
        if (item.name == "分类") {
          return uni.switchTab({
            url: "/pages/cate/cate"
          })
        }
      },
      // 获取楼层数据
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get("/api/public/v1/home/floordata");
        // console.log(res)
        if (res.meta.status != 200) {
          return uni.$showMsg()
        }
        // 跳转到商品列表页
        // 处理url地址：通过双层foreach循环
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            return prod.url = "/subpkg/goods_list/goods_list?" + prod.navigator_url.split('?')[1]
            // console.log(prod)
          })
        })
        this.floorList = res.message
      },
      goToSearch() {
        // console.log('search自定义事件')
        uni.navigateTo({
          'url':'/subpkg/search/search'
        });
      }
    }
  }
</script>

<style lang="scss">
  /* 搜索框的吸顶效果 */
  .search-box{
    position:sticky;
    top:0;
    z-index: 999;
  }
  swiper {
    height: 330rpx;

    .swiper-item {
      width: 100%;
      height: 100%;

      image {
        width: 100%;
        height: 100%;
      }
    }
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15px 0;

    image {
      width: 128rpx;
      height: 140rpx
    }
  }

  .floor-title {
    height: 60px;
    width: 100%;
    display: flex;
  }

  .floor-img-box {
    display: flex;
    padding-left: 15rpx;

    .right-img-box {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;

      /* image {
        margin-left: 10rpx;
      } */
    }
  }
</style>
